package com.ruoyi.system.service.impl;

import java.util.List;

import com.ruoyi.system.annotation.UserContext;
import com.ruoyi.system.domain.vo.UserData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.core.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.TbHomeRecommendsProduct;
import com.ruoyi.system.mapper.TbAppletHomepageInfoMapper;
import com.ruoyi.system.domain.TbAppletHomepageInfo;
import com.ruoyi.system.service.ITbAppletHomepageInfoService;

/**
 * 小程序首页信息管理 Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-09-11
 */
@Service
public class TbAppletHomepageInfoServiceImpl implements ITbAppletHomepageInfoService 
{
    @Autowired
    private TbAppletHomepageInfoMapper tbAppletHomepageInfoMapper;



    @Override
    public int configureRecommenCommodities(long homepageId, List<Long> selectedProducts) {

        // 使用 userData
        try {

            // 创建并初始化数组
            Long[] homepageIds = {homepageId}; // 数组直接初始化值

            UserData userData = UserContext.getUserData();

            String tableName = "tb_home_recommends_product" +  userData.getTableName();
//        先删
            tbAppletHomepageInfoMapper.deleteTbHomeRecommendsProductByHomepageIds(tableName,homepageIds);

            List<TbHomeRecommendsProduct> productList = new ArrayList<>();

            for (Long productId : selectedProducts) {
                TbHomeRecommendsProduct product = new TbHomeRecommendsProduct();
                product.setProductId(productId); // 设置商品 ID
                // 假设 homepageId 是某个固定值或另一个变量
                product.setHomepageId(homepageId); // 示例 homepageId
                productList.add(product);
            }

//        再插入
            return tbAppletHomepageInfoMapper.batchTbHomeRecommendsProduct(tableName, productList);
        }finally {
            // 清理上下文，确保即使发生异常也能执行清理
            UserContext.clear();
        }

    }

    @Override
    public List<TbHomeRecommendsProduct> selectTbHomeRecommendsProductList(Long homepageId) {




        // 使用 userData
        try {

            UserData userData = UserContext.getUserData();

            String tableName = "tb_home_recommends_product" +  userData.getTableName();

            return tbAppletHomepageInfoMapper.selectTbHomeRecommendsProductList(tableName,homepageId);
        }finally {
            // 清理上下文，确保即使发生异常也能执行清理
            UserContext.clear();
        }
    }

    /**
     * 查询小程序首页信息管理 
     * 
     * @param homepageId 小程序首页信息管理 主键
     * @return 小程序首页信息管理 
     */
    @Override
    public TbAppletHomepageInfo selectTbAppletHomepageInfoByHomepageId(Long homepageId)
    {


        // 使用 userData
        try {
            UserData userData = UserContext.getUserData();

            int merchantId = userData.getMerchantId();

            int storeId = userData.getStoreId();

            String tableName = "tb_applet_homepage_info" +  userData.getTableName();


            return tbAppletHomepageInfoMapper.selectTbAppletHomepageInfoByHomepageId(tableName,merchantId,storeId,homepageId);
        }finally {
            // 清理上下文，确保即使发生异常也能执行清理
            UserContext.clear();
        }


    }

    /**
     * 查询小程序首页信息管理 列表
     * 
     * @param tbAppletHomepageInfo 小程序首页信息管理 
     * @return 小程序首页信息管理 
     */
    @Override
    public List<TbAppletHomepageInfo> selectTbAppletHomepageInfoList(TbAppletHomepageInfo tbAppletHomepageInfo)
    {

        // 使用 userData
        try {
            UserData userData = UserContext.getUserData();

            tbAppletHomepageInfo.setMerchantId((long) userData.getMerchantId());

            tbAppletHomepageInfo.setStoreId((long) userData.getStoreId());

            tbAppletHomepageInfo.setTableName("tb_applet_homepage_info" +  userData.getTableName());

            return tbAppletHomepageInfoMapper.selectTbAppletHomepageInfoList(tbAppletHomepageInfo);
        }finally {
            // 清理上下文，确保即使发生异常也能执行清理
            UserContext.clear();
        }

    }

    /**
     * 新增小程序首页信息管理 
     * 
     * @param tbAppletHomepageInfo 小程序首页信息管理 
     * @return 结果
     */
    @Transactional
    @Override
    public int insertTbAppletHomepageInfo(TbAppletHomepageInfo tbAppletHomepageInfo)
    {

        // 使用 userData
        try {
            UserData userData = UserContext.getUserData();

            tbAppletHomepageInfo.setMerchantId((long) userData.getMerchantId());

            tbAppletHomepageInfo.setStoreId((long) userData.getStoreId());

            tbAppletHomepageInfo.setTableName("tb_applet_homepage_info" + userData.getTableName());

            int rows = tbAppletHomepageInfoMapper.insertTbAppletHomepageInfo(tbAppletHomepageInfo);

            insertTbHomeRecommendsProduct(tbAppletHomepageInfo);

            return rows;
        }finally {
            // 清理上下文，确保即使发生异常也能执行清理
            UserContext.clear();
        }


    }

    /**
     * 修改小程序首页信息管理 
     * 
     * @param tbAppletHomepageInfo 小程序首页信息管理 
     * @return 结果
     */
    @Transactional
    @Override
    public int updateTbAppletHomepageInfo(TbAppletHomepageInfo tbAppletHomepageInfo)
    {
        // 使用 userData
        try {
            UserData userData = UserContext.getUserData();

            tbAppletHomepageInfo.setMerchantId((long) userData.getMerchantId());

            tbAppletHomepageInfo.setStoreId((long) userData.getStoreId());

            tbAppletHomepageInfo.setTableName("tb_home_recommends_product" + userData.getTableName());


            tbAppletHomepageInfoMapper.deleteTbHomeRecommendsProductByHomepageId(tbAppletHomepageInfo.getTableName(),tbAppletHomepageInfo.getHomepageId());


            insertTbHomeRecommendsProduct(tbAppletHomepageInfo);

            tbAppletHomepageInfo.setTableName1("tb_applet_homepage_info" + userData.getTableName());

            return tbAppletHomepageInfoMapper.updateTbAppletHomepageInfo(tbAppletHomepageInfo);
        }finally {
            // 清理上下文，确保即使发生异常也能执行清理
            UserContext.clear();
        }


    }

    /**
     * 批量删除小程序首页信息管理 
     * 
     * @param homepageIds 需要删除的小程序首页信息管理 主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbAppletHomepageInfoByHomepageIds(Long[] homepageIds)
    {
        // 使用 userData
        try {
            UserData userData = UserContext.getUserData();

            int merchantId = userData.getMerchantId();

            int storeId = userData.getStoreId();

            String tableName = "tb_home_recommends_product" +  userData.getTableName();

            String tableName1 = "tb_applet_homepage_info" +  userData.getTableName();


            tbAppletHomepageInfoMapper.deleteTbHomeRecommendsProductByHomepageIds(tableName,homepageIds);
            return tbAppletHomepageInfoMapper.deleteTbAppletHomepageInfoByHomepageIds(tableName1,homepageIds);
        }finally {
            // 清理上下文，确保即使发生异常也能执行清理
            UserContext.clear();
        }


    }

    /**
     * 删除小程序首页信息管理 信息
     * 
     * @param homepageId 小程序首页信息管理 主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteTbAppletHomepageInfoByHomepageId(Long homepageId)
    {

        // 使用 userData
        try {
            UserData userData = UserContext.getUserData();

            int merchantId = userData.getMerchantId();

            int storeId = userData.getStoreId();

            String tableName = "tb_home_recommends_product" +  userData.getTableName();

            String tableName1 = "tb_applet_homepage_info" +  userData.getTableName();


            tbAppletHomepageInfoMapper.deleteTbHomeRecommendsProductByHomepageId(tableName, homepageId);
            return tbAppletHomepageInfoMapper.deleteTbAppletHomepageInfoByHomepageId(tableName1,homepageId);
        }finally {
            // 清理上下文，确保即使发生异常也能执行清理
            UserContext.clear();
        }


    }



    /**
     * 新增首页推荐商品信息
     * 
     * @param tbAppletHomepageInfo 小程序首页信息管理 对象
     */
    public void insertTbHomeRecommendsProduct(TbAppletHomepageInfo tbAppletHomepageInfo)
    {

        List<TbHomeRecommendsProduct> tbHomeRecommendsProductList = tbAppletHomepageInfo.getTbHomeRecommendsProductList();
        String tableName =  tbAppletHomepageInfo.getTableName();
        Long homepageId = tbAppletHomepageInfo.getHomepageId();

        if (StringUtils.isNotNull(tbHomeRecommendsProductList))
        {
            List<TbHomeRecommendsProduct> list = new ArrayList<TbHomeRecommendsProduct>();
            for (TbHomeRecommendsProduct tbHomeRecommendsProduct : tbHomeRecommendsProductList)
            {
                tbHomeRecommendsProduct.setHomepageId(homepageId);
                list.add(tbHomeRecommendsProduct);
            }
            if (list.size() > 0)
            {
                tbAppletHomepageInfoMapper.batchTbHomeRecommendsProduct(tableName,list);
            }
        }
    }
}
